package xiaohong.data.linked;

public class LinkedNodeTwoDemo {

    public static void main(String[] args) {
//        SingleLinkedNodeTwo singleLinkedNodeTwo = new SingleLinkedNodeTwo();
        HeroEntityTwo hero = new HeroEntityTwo(1,"小宏","神");
        HeroEntityTwo hero1 = new HeroEntityTwo(2,"斯塔克","漫威钢铁侠");
        HeroEntityTwo hero2 = new HeroEntityTwo(4,"Jack","美国队长");
        HeroEntityTwo hero6 = new HeroEntityTwo(8,"鸣人","火影");
        HeroEntityTwo hero3 = new HeroEntityTwo(6,"孙悟空","齐天大圣");
        HeroEntityTwo hero4 = new HeroEntityTwo(5,"杨戬","二郎神");
        HeroEntityTwo hero5 = new HeroEntityTwo(3,"共产党","傻逼");

        SingleLinkedNodeTwo.addHero(hero);
        SingleLinkedNodeTwo.addHero(hero1);
        SingleLinkedNodeTwo.addHero(hero2);
        SingleLinkedNodeTwo.addHero(hero3);
        SingleLinkedNodeTwo.addHero(hero4);
        SingleLinkedNodeTwo.addHero(hero5);
        SingleLinkedNodeTwo.addHero(hero6);
        SingleLinkedNodeTwo.delHero(3);
        SingleLinkedNodeTwo.list();
        hero5.setName("拦路与");
        hero5.setNickName("jack");
        SingleLinkedNodeTwo.updateHero(hero);
        SingleLinkedNodeTwo.list();
        SingleLinkedNodeTwo.delHero(5);
        SingleLinkedNodeTwo.list();
    }

}

class SingleLinkedNodeTwo {

    public SingleLinkedNodeTwo() {}

    static HeroEntityTwo head = new HeroEntityTwo(0, "", "");

    public static void addHero(HeroEntityTwo hero) {
        HeroEntityTwo heroEntityTwo = head;
        while (heroEntityTwo.getNext()!= null) {
            if (heroEntityTwo.getNext().getNo() > hero.getNo()) {
                break;
            }
            heroEntityTwo = heroEntityTwo.getNext();
        }
        HeroEntityTwo next = heroEntityTwo.getNext();
        heroEntityTwo.setNext(hero);
        hero.setPre(heroEntityTwo);
        hero.setNext(next);
        if (next == null) {
            return;
        }
        next.setPre(hero);
    }






    /**
     * 输出英雄
     */
    public static void list(){
        if (head.getNext() == null){
            System.out.println("链表为空");
            return;
        }
        HeroEntityTwo heroEntity = head.getNext();
        while (heroEntity != null) {
            // 输出英雄
            System.out.println(heroEntity);
            heroEntity = heroEntity.getNext();
        }
    }

    public static void updateHero(HeroEntityTwo heroEntity){
        boolean flag = false;
        HeroEntityTwo heroNext = head;
        while (heroNext.getNext() != null) {
            if (heroNext.getNext() == null){
                break;
            }
            if (heroNext.getNext().getNo() == heroEntity.getNo()) {
                flag = true;
                break;
            }
            heroNext = heroNext.getNext();
        }
        if (flag){
            System.out.println("修改的节点是：" + heroNext.getNext());
            heroNext.setNext(heroEntity);
            heroEntity.setPre(heroNext);
        }else {
            System.out.println("没有找到修改的节点");
        }

    }


    /**
     * 删除英雄
     * @param no 英雄编号
     */
    public static void delHero(int no){
        boolean flag = false;
        HeroEntityTwo heroNext = head.getNext();
        while (heroNext!= null) {
            if (heroNext.getNo() == no) {
                flag = true;
                break;
            }
            heroNext = heroNext.getNext();
        }
        if (flag){
            System.out.println("删除的节点是：" + heroNext);
            heroNext.getPre().setNext(heroNext.getNext());
            if (heroNext.getNext() == null){
                return;
            }
            heroNext.getNext().setPre(heroNext.getPre());
        }else {
            System.out.println("没有找到删除的节点");
        }
    }



}




class HeroEntityTwo{

    private HeroEntityTwo pre ;
    private int no ;
    private String name ;
    private String nickName ;
    private HeroEntityTwo next;

    public HeroEntityTwo() {}

    public HeroEntityTwo(int no, String name, String nickName) {
        this.no = no;
        this.name = name;
        this.nickName = nickName;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }

    public HeroEntityTwo getPre() {
        return pre;
    }

    public void setPre(HeroEntityTwo pre) {
        this.pre = pre;
    }

    public HeroEntityTwo getNext() {
        return next;
    }

    public void setNext(HeroEntityTwo next) {
        this.next = next;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }




}
